import QtQuick
import QtQuick.Controls
import QtQuick.Shapes

import "../as_qml"

Window {
    id: createCsvWin
    width: 640
    height: 480
    visible: true
    title: qsTr("生成CSV文件")

    property int leftSideListWidth: 150
    property int rightBottomSettingHeight: 150

    function addModelData(meetingName,fileName){
        var index = findIndex(meetingName)
        if(index === -1){
            directoryTreeModel.append({"meetingName":meetingName,"subNode":[{"fileName":fileName}]})
        }
        else{
            directoryTreeModel.get(index).subNode.append({"fileName":fileName})
        }

    }

    function findIndex(name){
        for(var i = 0 ; i < directoryTreeModel.count ; ++i){
            if(directoryTreeModel.get(i).meetingName === name){
                return i
            }
        }
        return -1
    }

    function setTipsShow(flag, text) {
        tipsText.color = (flag === true) ? "green" : "red"
        tipsText.text = qsTr(text)
    }

    Connections {
        target: myHandle

        function onRootFileDirectoryOpened(filePath, fileList) {
            var dirList = [""]
            var dirEntryList = [""]

            directoryTreeModel.clear()
            dirList = fileList
            for(var i = 0; i < dirList.length; i++) {
                dirEntryList = myHandle.getDirEntryList(filePath + "/" + dirList[i])
                for(var j = 0; j < dirEntryList.length; j++) {
                    addModelData(dirList[i], dirEntryList[j])
                }
            }
        }
    }

    Rectangle {
        id: directoryTreeRect
        width: leftSideListWidth
        anchors.top: parent.top
        anchors.bottom: tipsBottomRect.top
        anchors.left: parent.left

        Shape {
            anchors.fill: parent

            ShapePath {
                strokeColor: "#707070"
                startX: directoryTreeRect.width
                startY: 0

                PathLine {
                    x: directoryTreeRect.width
                    y: directoryTreeRect.height
                }
            }
        }

        AsButton {
            id: directoryTreeExpendBtn
            x: (directoryTreeRect.width - width ) / 2
            width: 80
            height: 0
            text: qsTr("一键展开")
            visible: false

            onClicked: {
                directoryTree.reuseItems = true
                directoryTree.cacheBuffer = directoryTreeModel.count * 2
                for(var i = 0; i < directoryTreeModel.count; i++) {
                    directoryTree.contentItem.children[i].itemExpend(false)
                }
            }
        }

        ListView {
            id: directoryTree
            width: parent.width
            anchors.top: directoryTreeExpendBtn.bottom
            anchors.bottom: parent.bottom
            spacing: 4
            clip: true
//            cacheBuffer: 100
//            reuseItems: true

            model: ListModel {
                id: directoryTreeModel
            }
            delegate: directoryTreeDelegate

            ScrollBar.vertical: AsScrollBar {}
        }

        Component {
            id: directoryTreeDelegate

            Column {
                id: objColumn
                width: directoryTree.width

                function itemExpend(visible1) {
                    for(var i = 1; i < children.length - 1; ++i) {
                        children[i].visible = !visible1
                    }
                    if(!visible1){
                        iconAin.from = 0
                        iconAin.to = 90
                        iconAin.start()
                    }
                    else{
                        iconAin.from = 90
                        iconAin.to = 0
                        iconAin.start()
                    }
                }

                Component.onCompleted: {
                    for(var i = 1; i < objColumn.children.length - 1; ++i) {
                        objColumn.children[i].visible = false
                    }
                }

                MouseArea{
                    id: mouseArea
                    width: parent.width
                    height: 20

                    onClicked: {
                        itemExpend(parent.children[1].visible)
                    }

                    RotationAnimation{
                        id: iconAin
                        target: icon
                        duration: 100
                    }

                    Row {
                        id: objItem
                        width: parent.width
                        height: parent.height
                        spacing: 2
                        leftPadding: 2

                        Image {
                            id: icon
                            width: 20
                            height: 20
                            source: "qrc:/img/unfold.png"
                            anchors.verticalCenter: parent.verticalCenter
                        }

                        AsText {
                            id: meeting_name
                            width: parent.width - icon.width
                            height: parent.height
                            text: meetingName
                            verticalAlignment: Text.AlignVCenter
                        }

                    }
                }

                Repeater {
                    id: itemRepeater
                    model: subNode

                    delegate: AsText {
                        x: 34
                        width: directoryTree.width - x
                        height: 20
                        text: fileName
                        verticalAlignment: Text.AlignVCenter
                    }

                }
            }
        }
    }

    Rectangle {
        id: rootShowRect
        anchors.top: parent.top
        anchors.bottom: btnColumn.top
        anchors.left: directoryTreeRect.right
        anchors.right: parent.right
        color: "#404040"

        Row {
            anchors.fill: parent
            padding: 8
            spacing: 8

            Image {
                width: parent.width / 3 * 2 - parent.padding - parent.spacing / 2
                height: parent.height - parent.padding * 2
                cache: false
                source: "qrc:/img/root.png"
            }

            AsText {
                width: parent.width / 3 - parent.padding - parent.spacing / 2
                height: parent.height - parent.padding * 2
                color: "#ffffff"
                text: qsTr("根目录文件夹展示
ORL_92x112
│
├─s1
│  ├s1_1.bmp
│  ├...
│  └s1_9.bmp
│
├─s?
│  ├s?_1.bmp
│  ├...
│  └s?_9.bmp
│
└─s40
    ├s40_1.bmp
    ├...
    └s40_9.bmp")
            }
        }
    }

    Column {
        id: btnColumn
        height: rightBottomSettingHeight
        anchors.bottom: tipsBottomRect.top
        anchors.left: directoryTreeRect.right
        anchors.right: parent.right
        padding: 4
        spacing: 4

        AsButton {
            id: openCSVFileBtn
            width: 100
            height: 28
            text: qsTr("打开图片路径")

            onClicked: {
                var ret = myHandle.openRootFileDir()
                setTipsShow(ret, (ret === false) ? "请选择正确的根目录文件夹,详见图文展示" : "无")
            }
        }

        AsButton {
            id: saveCSVFileBtn
            width: 100
            height: 28
            text: qsTr("保存CSV文件")

            onClicked: {
                var ret = myHandle.saveCSVFile()
                setTipsShow(ret, (ret === false) ? "操作取消" : "无")
            }
        }
    }

    Rectangle {
        id: tipsBottomRect
        width: parent.width
        height: 28
        anchors.bottom: parent.bottom
        color: "#e0e0e0"

        Row {
            anchors.fill: parent

            AsText {
                id: tipsTitle
                width: 40
                height: parent.height
                text: qsTr("提示:")
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
            }

            AsText {
                id: tipsText
                width: parent.width - tipsTitle.width
                height: parent.height
                text: qsTr("无")
                color: "green"
                verticalAlignment: Text.AlignVCenter
            }
        }
    }

}
